import AntModal from '@/components/pt/dialog/AntModal'
import SelectUser from '@/components/pt/selectUser/SelectUser'
import SelectRegion from '@/views/platform/components/SelectRegion'
import SelectPost from '@/views/platform/components/SelectPost'
import { getNotification, addNotification, updateNotification } from '@/api/platform/notification'
import { getUser } from '@/api/system/user'

export default {
  name: 'CreateForm',
  props: {
    typeOptions: {
      type: Array,
      required: true
    }
  },
  components: {
    AntModal,
    SelectUser,
    SelectPost,
    SelectRegion
  },
  data() {
    return {
      open: false,
      closeDialog: true,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      disabled: false,
      total: 0,
      id: undefined,
      formTitle: '添加推送表',
      // 表单参数
      form: {},
      rules: {
        itemId: [{ required: false, message: '请选择帖子', trigger: 'blur' }],
        type: [{ required: true, message: '请选择通知类型', trigger: 'blur' }],
        title: [{ required: false, message: '通知标题不能为空', trigger: 'blur' }],
        content: [{ required: false, message: '通知内容不能为空', trigger: 'blur' }]
      }
    }
  },
  filters: {},
  created() {},
  computed: {},
  watch: {
    'form.type': {
      handler(newVal, oldVal) {
        if (newVal === '0') {
          this.rules.itemId[0].required = false
          this.rules.title[0].required = true
          this.rules.content[0].required = true
        } else if (newVal === '1') {
          this.rules.itemId[0].required = true
          this.rules.title[0].required = false
          this.rules.content[0].required = false
        }
      }
    }
  },
  mounted() {},
  methods: {
    onClose() {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,

        regionId: undefined,

        itemId: undefined,

        userId: undefined,

        title: undefined,

        content: undefined,

        type: undefined
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.formTitle = '添加推送表'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.open = true
      this.spinning = !this.spinning
      const notificationId = row.id
      getNotification(notificationId).then(response => {
        this.form = response.data
        if (this.form.userId !== null) {
          getUser(this.form.userId).then(res => {
            this.form.userId = { ids: this.form.userId, names: res.data.name }
          })
        }
        this.formTitle = '修改推送表'
        this.spinning = !this.spinning
      })
    },
    /** 提交按钮 */
    submitForm: function (closeDialog) {
      this.closeDialog = closeDialog
      this.disabled = true
      this.$refs.form.validate(valid => {
        if (valid) {
          const saveForm = JSON.parse(JSON.stringify(this.form))
          if (this.form.userId !== null && this.form.userId !== undefined) {
            saveForm.userId = this.form.userId.ids
          }
          if (this.form.itemId !== null && this.form.itemId !== undefined) {
            saveForm.regionId = this.$refs.selectPost.getChoosePost().regionId
          }
          if (this.form.id !== undefined) {
            updateNotification(saveForm).then(response => {
              this.$message.success('更新成功', 3)
              this.open = false
              this.$emit('ok')
              this.$emit('close')
              this.disabled = false
            })
          } else {
            addNotification(saveForm).then(response => {
              this.$message.success('新增成功', 3)
              this.open = false
              this.$emit('ok')
              this.$emit('close')
              this.disabled = false
            })
          }
        } else {
          this.disabled = false
          return false
        }
      })
    },
    back() {
      const index = '/platform/notification/index'
      this.$router.push(index)
    }
  }
}
